package com.msb.servicemap.remote;

import com.msb.internalcommon.constant.AmapConfigConstants;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.reponese.ServiceResponse;
import com.msb.internalcommon.reponese.TerminalResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: jiudianliu
 * @Date: 2023/7/8 - 07 - 08 - 9:46
 * @Description: com.msb.servicemap.remote
 * @version: 1.0
 */
@Service
public class TerminalClient {

    @Value("${map.key}")
    private String key ;

    @Value("${map.sid}")
    private String sid ;

    @Autowired
    private RestTemplate restTemplate ;

    public ResponseResult<TerminalResponse> add (String name , String desc) {
        //拼装请求的url
        //网址示例：?keywords=北京&subdistrict=2&key=<用户的key>
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.TERMINAL_ADD_URL);
        url.append("?");
        url.append("key="+key);
        url.append("&");
        url.append("sid="+sid);
        url.append("&");
        url.append("name="+name);
        url.append("&");
        url.append("desc=" + desc);
        System.out.println("创建终端请求：" + url.toString());
        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null , String.class);
        System.out.println("创建终端响应：" + forEntity.getBody());
        String body = forEntity.getBody();
        JSONObject jsonObject = JSONObject.fromObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        String tid = data.getString("tid");
        TerminalResponse terminalResponse = new TerminalResponse();
        terminalResponse.setTid(tid);
        return ResponseResult.success(terminalResponse) ;
    }

    public ResponseResult<List<TerminalResponse>> aroundsearch (String center , Integer radius){
        //拼装请求的url
        //网址示例：?keywords=北京&subdistrict=2&key=<用户的key>
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.TERMINAL_AROUNDSEARCH);
        url.append("?");
        url.append("key="+key);
        url.append("&");
        url.append("sid="+sid);
        url.append("&");
        url.append("center="+center);
        url.append("&");
        url.append("radius=" + radius);
        System.out.println("终端搜索请求：" + url.toString());
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url.toString(), null, String.class);
        System.out.println("终端搜索响应" + stringResponseEntity.getBody());

        // 解析终端搜索结果
        String body = stringResponseEntity.getBody();
        JSONObject bodyJsonObject = JSONObject.fromObject(body);
        JSONObject data = bodyJsonObject.getJSONObject("data");


        List<TerminalResponse> terminalResponseList = new ArrayList<>();

        TerminalResponse terminalResponse = new TerminalResponse();

        JSONArray results = data.getJSONArray("results");
        for (int i = 0 ; i < results.size() ; i++){
            JSONObject jsonObject1 = results.getJSONObject(i);
            // desc 是 carId
            String desc = jsonObject1.getString("desc");
            Long carId = Long.parseLong(desc);
            String tid = jsonObject1.getString("tid");

            JSONObject location = jsonObject1.getJSONObject("location");
            String longitude = location.getString("longitude");
            String latitude = location.getString("latitude");

            terminalResponse.setCarId(carId);
            terminalResponse.setTid(tid);
            terminalResponse.setLatitude(latitude);
            terminalResponse.setLongitude(longitude);
        }
        terminalResponseList.add(terminalResponse);

        return ResponseResult.success(terminalResponseList);
    }
}
